#include "money.h"
#include "ui_money.h"

money::money(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::money)
{
    ui->setupUi(this);
    QSqlDatabase db= QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("127.0.0.1");
    db.setDatabaseName("restaurant");
    db.setUserName("root");
    db.setPassword("123456");
    bool ret = db.open();
    if(!ret)
    {
        qDebug()<<"open error";
    }
}

money::~money()
{
    delete ui;
}



void money::on_allToolButton_clicked()
{ 
    QDateTime dateTime= QDateTime::currentDateTime();//获取系统当前的时间

    QString str_year = dateTime .toString("yyyy");//格式化时间
    QString str_month = dateTime .toString("MM");//格式化时间
    QString str_day = dateTime .toString("dd");//格式化时间
     qDebug() << "str:" << str_year<<str_month<<str_day;

    //切换页面
    ui->stackedWidget->setCurrentIndex(0);
     // 生成数据，画出的是抛物线
     //清除之前
     ui->allCustomPlot->clearGraphs();
/*
     //连接数据库
     QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
     db.setHostName("localhost");
     //sqlite数据库不需要设置用户名和密码
     db.setUserName("root");
     db.setPassword("123456");
     db.setDatabaseName("restaurant");
     if (!db.open()) {
         qDebug() << "Failed to connect to database.";
         return;
     }
*/
     // 执行SQL语句
     QSqlQuery query;
     /*query.exec("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER)");
     query.exec("INSERT INTO users (name, age) VALUES ('Alice', 25)");
     query.exec("INSERT INTO users (name, age) VALUES ('Bob', 30)");*/
    //拼接字符串
     if(flag == 0)
     {
     QString str_query = QString("SELECT sum(money), date_hour FROM turnover WHERE date_year = '%1' AND date_month = '%2' AND date_day = '%3' GROUP BY date_hour;").arg(str_year).arg(str_month).arg("05");

     qDebug() << "str_query"<<str_query;

     // 获取数据
     query.exec(str_query);

     //绘制折线图
     QVector<double> x(101), y(101);
     int i = 0;
     int max = 0;
     int min = 0;
     //初始化向量x和y
     /*for (int i=0; i<101; ++i)
     {
       x[i] = i/50.0 - 1; // x范围[-1,1]
       y[i] = x[i]*x[i]; // y=x*x
     }*/
     while (query.next()) {

         int money = query.value(0).toFloat();
         //int kind = query.value(1).toInt();
        // int set_menu = query.value(2).toInt();
         //QString year = query.value(3).toString();
        // QString month = query.value(4).toString();
         //QString day = query.value(5).toString();
         QString hour = query.value(1).toString();
            qDebug() << "money"<<money<<hour;
         if(max < money)
         {
             max = money;
         }else if(min > money)
         {
             min = money;
         }

         // qDebug() << "User:" << money << kind << set_menu << year <<month<<day;
         x[i] = hour.toInt();
         y[i] = money;
         i++;

     }

     ui->allCustomPlot->addGraph();//添加数据曲线（一个图像可以有多个数据曲线）

     // graph(0);可以获取某个数据曲线（按添加先后排序）
     // setData();为数据曲线关联数据
     ui->allCustomPlot->graph(0)->setData(x, y);
     ui->allCustomPlot->graph(0)->setName("总营业额");// 设置图例名称
     // 为坐标轴添加标签
     ui->allCustomPlot->xAxis->setLabel("小时");
     ui->allCustomPlot->yAxis->setLabel("营业额");
     // 设置坐标轴的范围，以看到所有数据
     ui->allCustomPlot->xAxis->setRange(10, 20);
     ui->allCustomPlot->yAxis->setRange(min, max);

     ui->allCustomPlot->legend->setVisible(true); // 显示图例
     // 重画图像
     ui->allCustomPlot->replot();
     }else if(flag == 1)
     {

         QString str_query = QString("SELECT sum(money), date_day FROM turnover WHERE date_year = '%1' AND date_month = '%2' AND date_day BETWEEN '%3' AND '%4' GROUP BY date_day;").arg(str_year).arg(str_month).arg("01").arg("06");

         qDebug() << "str_query"<<str_query;

         //获取数据
         query.exec(str_query);

         //绘制折线图
         QVector<double> x(101), y(101);
         int i = 0;
         int max = 0;
         int min = 0;
         //初始化向量x和y
         /*for (int i=0; i<101; ++i)
         {
           x[i] = i/50.0 - 1; // x范围[-1,1]
           y[i] = x[i]*x[i]; // y=x*x
         }*/
         while (query.next()) {

             int money = query.value(0).toFloat();
             //int kind = query.value(1).toInt();
            // int set_menu = query.value(2).toInt();
             //QString year = query.value(3).toString();
            // QString month = query.value(4).toString();
             //QString day = query.value(5).toString();
             QString day = query.value(1).toString();
                qDebug() << "money"<<money<<day;
             if(max < money)
             {
                 max = money;
             }else if(min > money)
             {
                 min = money;
             }

             // qDebug() << "User:" << money << kind << set_menu << year <<month<<day;
             x[i] = day.toInt();
             y[i] = money;
             i++;

         }

         ui->allCustomPlot->addGraph();//添加数据曲线（一个图像可以有多个数据曲线）

         // graph(0);可以获取某个数据曲线（按添加先后排序）
         // setData();为数据曲线关联数据
         ui->allCustomPlot->graph(0)->setData(x, y);
         ui->allCustomPlot->graph(0)->setName("总营业额");// 设置图例名称
         // 为坐标轴添加标签
         ui->allCustomPlot->xAxis->setLabel("天");
         ui->allCustomPlot->yAxis->setLabel("营业额");
         // 设置坐标轴的范围，以看到所有数据
         ui->allCustomPlot->xAxis->setRange(1, 7);
         ui->allCustomPlot->yAxis->setRange(min, max);
         ui->allCustomPlot->legend->setVisible(true); // 显示图例
         // 重画图像
         ui->allCustomPlot->replot();
     }else if(flag == 2)
     {

         QString str_query = QString("SELECT sum(money), date_day FROM turnover WHERE date_year = '%1' AND date_month = '%2' AND date_day BETWEEN '%3' AND '%4' GROUP BY date_day;").arg(str_year).arg(str_month).arg("01").arg("31");

         qDebug() << "str_query"<<str_query;

         //获取数据
         query.exec(str_query);

         //绘制折线图
         QVector<double> x(101), y(101);
         int i = 0;
         int max = 0;
         int min = 0;
         //初始化向量x和y
         /*for (int i=0; i<101; ++i)
         {
           x[i] = i/50.0 - 1; // x范围[-1,1]
           y[i] = x[i]*x[i]; // y=x*x
         }*/
         while (query.next()) {

             int money = query.value(0).toFloat();
             //int kind = query.value(1).toInt();
            // int set_menu = query.value(2).toInt();
             //QString year = query.value(3).toString();
            // QString month = query.value(4).toString();
             //QString day = query.value(5).toString();
             QString day = query.value(1).toString();
                qDebug() << "money"<<money<<day;
             if(max < money)
             {
                 max = money;
             }else if(min > money)
             {
                 min = money;
             }

             // qDebug() << "User:" << money << kind << set_menu << year <<month<<day;
             x[i] = day.toInt();
             y[i] = money;
             i++;

         }

         ui->allCustomPlot->addGraph();//添加数据曲线（一个图像可以有多个数据曲线）

         // graph(0);可以获取某个数据曲线（按添加先后排序）
         // setData();为数据曲线关联数据
         ui->allCustomPlot->graph(0)->setData(x, y);
         ui->allCustomPlot->graph(0)->setName("总营业额");// 设置图例名称
         // 为坐标轴添加标签
         ui->allCustomPlot->xAxis->setLabel("日");
         ui->allCustomPlot->yAxis->setLabel("营业额");
         // 设置坐标轴的范围，以看到所有数据
         ui->allCustomPlot->xAxis->setRange(1, 31);
         ui->allCustomPlot->yAxis->setRange(min, max);
         ui->allCustomPlot->legend->setVisible(true); // 显示图例
         // 重画图像
         ui->allCustomPlot->replot();
     }

}

void money::on_buyToolButton_clicked()
{
    QDateTime dateTime= QDateTime::currentDateTime();//获取系统当前的时间

    QString str_year = dateTime .toString("yyyy");//格式化时间
    QString str_month = dateTime .toString("MM");//格式化时间
    QString str_day = dateTime .toString("dd");//格式化时间
     qDebug() << "str:" << str_year<<str_month<<str_day;
    //切换页面
    ui->stackedWidget->setCurrentIndex(1);
     // 生成数据，画出的是抛物线
     //清除之前
     ui->allCustomPlot->clearGraphs();
     // 执行SQL语句
     // 执行SQL语句
     QSqlQuery query;
     /*query.exec("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER)");
     query.exec("INSERT INTO users (name, age) VALUES ('Alice', 25)");
     query.exec("INSERT INTO users (name, age) VALUES ('Bob', 30)");*/
    //拼接字符串
     QString str_query = QString("SELECT sum(money), date_hour FROM turnover WHERE date_year = '%1' AND date_month = '%2' AND date_day = '%3' GROUP BY date_hour;.").arg(str_year).arg(str_month).arg(str_day);
     qDebug() << "str_query"<<str_query;

     // 获取数据
     query.exec(str_query);

     //绘制折线图
     QVector<double> x(101), y(101);
     int i = 0;
     int max = 0;
     int min = 0;
     //初始化向量x和y
     /*for (int i=0; i<101; ++i)
     {
       x[i] = i/50.0 - 1; // x范围[-1,1]
       y[i] = x[i]*x[i]; // y=x*x
     }*/while (query.next()) {

         int money = query.value(0).toFloat();
         //int kind = query.value(1).toInt();
        // int set_menu = query.value(2).toInt();
         //QString year = query.value(3).toString();
        // QString month = query.value(4).toString();
         //QString day = query.value(5).toString();
         QString hour = query.value(1).toString();

         if(max < money)
         {
             max = money;
         }else if(min > money)
         {
             min = money;
         }

         // qDebug() << "User:" << money << kind << set_menu << year <<month<<day;
         x[i] = hour.toInt();
         y[i] = money;
         i++;

     }

     ui->allCustomPlot->addGraph();//添加数据曲线（一个图像可以有多个数据曲线）

     // graph(0);可以获取某个数据曲线（按添加先后排序）
     // setData();为数据曲线关联数据
     ui->allCustomPlot->graph(0)->setData(x, y);
     ui->allCustomPlot->graph(0)->setName("总营业额");// 设置图例名称
     // 为坐标轴添加标签
     ui->allCustomPlot->xAxis->setLabel("小时");
     ui->allCustomPlot->yAxis->setLabel("营业额");
     // 设置坐标轴的范围，以看到所有数据
     ui->allCustomPlot->xAxis->setRange(10, 20);
     ui->allCustomPlot->yAxis->setRange(min, max);
     ui->allCustomPlot->legend->setVisible(true); // 显示图例
     // 重画图像
     ui->allCustomPlot->replot();
}

void money::on_sceneToolButton_clicked()
{
    ui->stackedWidget->setCurrentIndex(2);
    // 生成数据，画出的是抛物线
    //清除之前
    ui->sceneCustomPlot->clearGraphs();
    QVector<double> x(101), y(101); //初始化向量x和y
    for (int i=0; i<101; ++i)
    {
      x[i] = i/50.0 - 1; // x范围[-1,1]
      y[i] = x[i]*x[i]; // y=x*x
    }
    ui->sceneCustomPlot->addGraph();//添加数据曲线（一个图像可以有多个数据曲线）

    // graph(0);可以获取某个数据曲线（按添加先后排序）
    // setData();为数据曲线关联数据
    ui->sceneCustomPlot->graph(0)->setData(x, y);
    ui->sceneCustomPlot->graph(0)->setName("总营业额");// 设置图例名称
    // 为坐标轴添加标签
    ui->sceneCustomPlot->xAxis->setLabel("x");
    ui->sceneCustomPlot->yAxis->setLabel("y");
    // 设置坐标轴的范围，以看到所有数据
    ui->sceneCustomPlot->xAxis->setRange(-1, 1);
    ui->sceneCustomPlot->yAxis->setRange(0, 1);
    ui->sceneCustomPlot->legend->setVisible(true); // 显示图例
    // 重画图像
    ui->sceneCustomPlot->replot();
}


void money::on_packageToolButton_clicked()
{
    QDateTime dateTime= QDateTime::currentDateTime();//获取系统当前的时间

    QString str_year = dateTime .toString("yyyy");//格式化时间
    QString str_month = dateTime .toString("MM");//格式化时间
    QString str_day = dateTime .toString("dd");//格式化时间
     qDebug() << "str:" << str_year<<str_month<<str_day;


    ui->stackedWidget_2->setCurrentIndex(0);
    //绘制柱状图


    ui->page_4->clearPlottables();

    QCPAxis *xAxis = ui->page_4->xAxis;//x轴
    QCPAxis *yAxis = ui->page_4->yAxis;//y轴
    QCPBars *bars = new QCPBars(xAxis, yAxis);  // 使用xAxis作为柱状图的x轴，yAxis作为y轴

    bars->setAntialiased(false); // 为了更好的边框效果，关闭抗齿锯

    bars->setName("销量套餐"); // 设置图例
    bars->setPen(QPen(QColor(0, 160, 140).lighter(130))); // 设置柱状图的边框颜色
    bars->setBrush(QColor(20,68,106));  // 设置柱状图的画刷颜色

    QVector<double> ticks;
    QVector<QString> labels;
    ticks << 1 << 2 << 3 << 4 << 5 << 6 << 7;//轴的范围
    labels << "海鲜盛宴" << "健康低卡" ;//轴的刻度文字显示
    QSharedPointer<QCPAxisTickerText> textTicker(new QCPAxisTickerText);
    textTicker->addTicks(ticks, labels);
    xAxis->setTicker(textTicker);        // 设置为文字轴
    xAxis->setTickLabelRotation(60);     // 轴刻度文字旋转60度
    xAxis->setSubTicks(false);           // 不显示子刻度
    xAxis->setTickLength(0, 4);          // 轴内外刻度的长度分别是0,4,也就是轴内的刻度线不显示
    xAxis->setRange(0, 8);               // 设置x轴范围
    xAxis->setLabel("x");
    xAxis->setUpperEnding(QCPLineEnding::esSpikeArrow);

    yAxis->setRange(0, 12.1);          //设置y轴范围
    yAxis->setPadding(35);             // 轴的内边距
    yAxis->setLabel("y");
    yAxis->setUpperEnding(QCPLineEnding::esSpikeArrow);
    QVector<double> fossilData;
    QSqlQuery query;// 执行SQL语句
    /*if(flag == 0)
         {
         QString str_query = QString("SELECT sum(money),set_menu from turnover where date_year = '%1' and date_month = '%2' and date_day = '05' and set_menu between 1 and 2 group by set_menu;").arg(str_year).arg(str_month);
         while (query.next()) {

                      int money = query.value(0).toFloat();

                      int set_menu = query.value(1).toInt();
                      qDebug() << "money"<<money<<set_menu;


                      // qDebug() << "User:" << money << kind << set_menu << year <<month<<day;


         }
*/
        fossilData  << 5<<8;//y轴坐标值
        bars->setData(ticks, fossilData);
    ui->page_4->legend->setVisible(true); // 显示图例
    // 重画图像
    ui->page_4->replot();



}

void money::on_dishesToolButton_clicked()
{
    ui->stackedWidget_2->setCurrentIndex(1);
    //绘制柱状图


    ui->page_5->clearPlottables();

    QCPAxis *xAxis = ui->page_5->xAxis;//x轴
    QCPAxis *yAxis = ui->page_5->yAxis;//y轴
    QCPBars *bars = new QCPBars(xAxis, yAxis);  // 使用xAxis作为柱状图的x轴，yAxis作为y轴

    bars->setAntialiased(false); // 为了更好的边框效果，关闭抗齿锯
    bars->setName("销量菜品"); // 设置图例
    bars->setPen(QPen(QColor(0, 160, 140).lighter(130))); // 设置柱状图的边框颜色
    bars->setBrush(QColor(20,68,106));  // 设置柱状图的画刷颜色

    QVector<double> ticks;
    QVector<QString> labels;
    ticks << 1 << 2 << 3 << 4 << 5 << 6 << 7;//轴的范围
    labels << "糖醋里脊" << "鱼香肉丝" << "小炒肉" << "宫保鸡丁" << "过油肉拌面" << "小馋猫烤肉" << "抓饭";//轴的刻度文字显示
    QSharedPointer<QCPAxisTickerText> textTicker(new QCPAxisTickerText);
    textTicker->addTicks(ticks, labels);
    xAxis->setTicker(textTicker);        // 设置为文字轴
    xAxis->setTickLabelRotation(60);     // 轴刻度文字旋转60度
    xAxis->setSubTicks(false);           // 不显示子刻度
    xAxis->setTickLength(0, 4);          // 轴内外刻度的长度分别是0,4,也就是轴内的刻度线不显示
    xAxis->setRange(0, 8);               // 设置x轴范围
    xAxis->setLabel("x");
    xAxis->setUpperEnding(QCPLineEnding::esSpikeArrow);

    yAxis->setRange(0, 12.1);          //设置y轴范围
    yAxis->setPadding(35);             // 轴的内边距
    yAxis->setLabel("y");
    yAxis->setUpperEnding(QCPLineEnding::esSpikeArrow);
    QVector<double> fossilData;
    fossilData  << 10 << 9 << 2 << 5 << 7 << 4 << 1;//y轴坐标值
    bars->setData(ticks, fossilData);
    ui->page_5->legend->setVisible(true); // 显示图例
    // 重画图像
    ui->page_5->replot();

}


void money::on_sexToolButton_4_clicked()
{
    ui->stackedWidget_3->setCurrentIndex(0);
    resize(400,300);
        //创建一个图表
    QChart *chart = new QChart;

        /*
        //创建坐标轴
        QValueAxis *AxisX = new QValueAxis;  //x坐标轴
        QValueAxis *AxisY = new QValueAxis;  //y坐标轴

        //设置坐标轴的范围
        AxisX->setRange(0,10);
        AxisY->setRange(0,50);

        //设置标签显示的格式
        AxisX->setLabelFormat("%.1lf");
        AxisY->setLabelFormat("%d");

        //设置坐标轴标题
        AxisX->setTitleText("时间/ms");
        AxisY->setTitleText("温度/℃");
        AxisY->setTickCount(6);
        AxisX->setTickCount(21);

        //图标添加坐标轴
        chart->createDefaultAxes();
        chart->addAxis(AxisX,Qt::AlignBottom);
        chart->addAxis(AxisY,Qt::AlignLeft);
        */

        //设置图标的标题
        chart->setTitle("会员性别");
        //设置图例可见
    //    chart->legend()->setVisible(true);

        QPieSeries *p_spline = new QPieSeries;
        QPieSlice *pslice1 = new QPieSlice("男",0.3);
        QPieSlice *pslice2 = new QPieSlice("女",0.7);

    //    p_spline->append("part1",0.4);
    //    p_spline->append("2",0.1);
    //    p_spline->append("3",0.3);
    //     p_spline->append("4",0.2);
        p_spline->append(pslice1);
        p_spline->append(pslice2);


        //设置图标动画
        chart->setAnimationOptions(QChart::AllAnimations);
        //设置鼠标按下 ，分离
        connect(pslice1,&QPieSlice::pressed,[=](){
            pslice1->setExploded(true);
        });
         //设置鼠标按下 ，合并
        connect(pslice1,&QPieSlice::released,[=](){
            pslice1->setExploded(false);
        });

        connect(pslice2,&QPieSlice::pressed,[=](){
            pslice2->setExploded(true);
        });
         //设置鼠标按下 ，合并
        connect(pslice2,&QPieSlice::released,[=](){
            pslice2->setExploded(false);
        });
 /*
        connect(pslice3,&QPieSlice::pressed,[=](){
            pslice3->setExploded(true);
        });
       //设置鼠标按下 ，合并
        connect(pslice3,&QPieSlice::released,[=](){
            pslice3->setExploded(false);
        });

        connect(pslice4,&QPieSlice::pressed,[=](){
            pslice4->setExploded(true);
        });
         //设置鼠标按下 ，合并
        connect(pslice4,&QPieSlice::released,[=](){
            pslice4->setExploded(false);
        });*/
        chart->addSeries(p_spline);

    //    p_spline->attachAxis(AxisX);
    //    p_spline->attachAxis(AxisY);

    ui->graphicsView->setChart(chart);

    //ui->page_6->legend->setVisible(true); // 显示图例
    // 重画图像
}

void money::on_ageToolButton_5_clicked()
{
    ui->stackedWidget_3->setCurrentIndex(1);
    resize(400,300);
        //创建一个图表
    QChart *chart = new QChart;

        /*
        //创建坐标轴
        QValueAxis *AxisX = new QValueAxis;  //x坐标轴
        QValueAxis *AxisY = new QValueAxis;  //y坐标轴

        //设置坐标轴的范围
        AxisX->setRange(0,10);
        AxisY->setRange(0,50);

        //设置标签显示的格式
        AxisX->setLabelFormat("%.1lf");
        AxisY->setLabelFormat("%d");

        //设置坐标轴标题
        AxisX->setTitleText("时间/ms");
        AxisY->setTitleText("温度/℃");
        AxisY->setTickCount(6);
        AxisX->setTickCount(21);

        //图标添加坐标轴
        chart->createDefaultAxes();
        chart->addAxis(AxisX,Qt::AlignBottom);
        chart->addAxis(AxisY,Qt::AlignLeft);
        */

        //设置图标的标题
        chart->setTitle("会员年龄");
        //设置图例可见
    //    chart->legend()->setVisible(true);

        QPieSeries *p_spline = new QPieSeries;
        QPieSlice *pslice1 = new QPieSlice("58以上",0.1);
        QPieSlice *pslice2 = new QPieSlice("18-28",0.4);
        QPieSlice *pslice3 = new QPieSlice("28-38",0.2);
        QPieSlice *pslice4 = new QPieSlice("38-58",0.3);
    //    p_spline->append("part1",0.4);
    //    p_spline->append("2",0.1);
    //    p_spline->append("3",0.3);
    //     p_spline->append("4",0.2);
        p_spline->append(pslice1);
        p_spline->append(pslice2);
        p_spline->append(pslice3);
        p_spline->append(pslice4);

        //设置图标动画
        chart->setAnimationOptions(QChart::AllAnimations);
        //设置鼠标按下 ，分离
        connect(pslice1,&QPieSlice::pressed,[=](){
            pslice1->setExploded(true);
        });
         //设置鼠标按下 ，合并
        connect(pslice1,&QPieSlice::released,[=](){
            pslice1->setExploded(false);
        });

        connect(pslice2,&QPieSlice::pressed,[=](){
            pslice2->setExploded(true);
        });
         //设置鼠标按下 ，合并
        connect(pslice2,&QPieSlice::released,[=](){
            pslice2->setExploded(false);
        });

        connect(pslice3,&QPieSlice::pressed,[=](){
            pslice3->setExploded(true);
        });
         //设置鼠标按下 ，合并
        connect(pslice3,&QPieSlice::released,[=](){
            pslice3->setExploded(false);
        });

        connect(pslice4,&QPieSlice::pressed,[=](){
            pslice4->setExploded(true);
        });
         //设置鼠标按下 ，合并
        connect(pslice4,&QPieSlice::released,[=](){
            pslice4->setExploded(false);
        });
        chart->addSeries(p_spline);

    //    p_spline->attachAxis(AxisX);
    //    p_spline->attachAxis(AxisY);

    ui->graphicsView_2->setChart(chart);

    //ui->page_6->legend->setVisible(true); // 显示图例
    // 重画图像
}

void money::on_tabWidget_currentChanged(int index)
{

    if(index == 0)
    {
        flag = 0;
    }else if(index == 1)
    {
        flag = 1;
    }else if(index == 2)
    {
        flag = 2;
    }
    qDebug() << "index:" << index <<flag;
}
